package com.example.administrator.text.Framgnet;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.text.R;
import java.util.ArrayList;
import java.util.List;
import entity.HomeZhuYe;
import util.BitmapUtil;
import util.JumpActivityUtils;
import util.LogUtil;

/**
 * 首页页ViewPager中的Fragment
 */
public class VPHomeFragment extends Fragment {
    public static final String POSITION = "POSITION";
    private static final String IMAGELIST = "IMAGELIST";
    private static final String VIEWPAGERHEAD = "VIEWPAGERHEAD";
    private String mPic;//图片地址
    private List<HomeZhuYe.BannerModelsBean.PicModelsBean> mImagelist;
    private List<HomeZhuYe.BannerModelsBean.TextModelsBean> mList;
    private TextView mMarqueeText1,mMarqueeText2,mMarqueeText3,mMarqueeText4,mMarqueeText5;//头部ViewPageer中的文字
    private View mView;
    private int mPosition;
    private static VPHomeFragment fragment;
    public VPHomeFragment(){

    }

    public static VPHomeFragment newInstance(ArrayList<HomeZhuYe.BannerModelsBean.PicModelsBean> imagelist, ArrayList<HomeZhuYe.BannerModelsBean.TextModelsBean> list, int position){

        fragment = new VPHomeFragment();
        Bundle args = new Bundle();
        args.putInt(POSITION, position);
        args.putParcelableArrayList(IMAGELIST, imagelist);
        args.putParcelableArrayList(VIEWPAGERHEAD, list);
        fragment.setArguments(args);
        return fragment;
    }

    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mPosition = getArguments().getInt(POSITION);
            mImagelist = getArguments().getParcelableArrayList(IMAGELIST);
            mList = getArguments().getParcelableArrayList(VIEWPAGERHEAD);
        }
    }

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //图片和字符串
        if(mList.size()>0){

            if(mPosition  == mImagelist.size()){
                mView = inflater.inflate(R.layout.home_textview, container, false);
                initString();

            }else if(mPosition  < mImagelist.size()){

                mPic = mImagelist.get(mPosition).getPic();
                LogUtil.eE("mPosition",mPic+"");
                mView = inflater.inflate(R.layout.home_image1,container,false);
                ImageView imageView = (ImageView)mView.findViewById(R.id.image_home_viewager);
                BitmapUtil.newBitmap().setBitmap(getActivity(),mPic,imageView,mPosition);
                //BitmapUtil.newBitmap().showBitmap(getActivity(),mPic,imageView,R.mipmap.pre_home_head,null);
                mView.setOnClickListener(new View.OnClickListener() {
                    @Override public void onClick(View v) {
                        LogUtil.eE("mPosition",mPosition+"");//http://wxvod.sewise.com/?mod=webview&do=vodtest&cid=317
                        JumpActivityUtils.getInstance().jumpWebshipoView(getActivity(),mImagelist.get(mPosition).getUrl(),mImagelist.get(mPosition).getText());
                    }});
            }

        }else if(mImagelist.size()>0){
            //只显示图片
            LogUtil.eE("mPosition",mPic+"");
            mPic = mImagelist.get(mPosition).getPic();
            mView = inflater.inflate(R.layout.home_image1,container,false);
            ImageView imageView = (ImageView)mView.findViewById(R.id.image_home_viewager);
           BitmapUtil.newBitmap().setBitmap(getActivity(),mPic,imageView,mPosition);
           // BitmapUtil.newBitmap().showBitmap(getActivity(),mPic,imageView,R.mipmap.pre_home_head,null);
            mView.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    LogUtil.eE("mPosition",mPosition+"");//http://wxvod.sewise.com/?mod=webview&do=vodtest&cid=317
                    JumpActivityUtils.getInstance().jumpWebshipoView(getActivity(),mImagelist.get(mPosition).getUrl(),mImagelist.get(mPosition).getText());
                }});
        }
        return mView;
    }

    /**
     * 初始化文字
     */
    private void initString() {
        mMarqueeText1 = (TextView) mView.findViewById(R.id.TextViewNoti1);
        mMarqueeText1.setText(mList.get(0).getText());
        mMarqueeText2 = (TextView) mView.findViewById(R.id.TextViewNoti2);
        mMarqueeText2.setText(mList.get(1).getText());
        mMarqueeText3 = (TextView) mView.findViewById(R.id.TextViewNoti3);
        mMarqueeText3.setText(mList.get(2).getText());
        mMarqueeText4 = (TextView) mView.findViewById(R.id.TextViewNoti4);
        mMarqueeText4.setText(mList.get(3).getText());
        mMarqueeText5 = (TextView) mView.findViewById(R.id.TextViewNoti5);
        mMarqueeText5.setText(mList.get(4).getText());
    }

    @Override public void onDestroy() {
        super.onDestroy();

        if(mList !=null){ mList.clear();}
        if(mImagelist !=null){ mImagelist.clear();}
    }




}
